package com.jiuhaohealth.log.launcher.handler;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import com.jiuhaohealth.log.launcher.conf.LauncherConfig;
import com.jiuhaohealth.log.launcher.model.RequestInfo;

public class LauncherUtil {
	private static LauncherConfig launchConfig = LauncherConfig.getInstance();
	private static ObjectMapper mapper = new ObjectMapper();
	
	public static void launch(String msg){
		launchConfig.client.launch(msg);
	}
	
	/**
	 * 收集整理请求信息
	 * @param event
	 * @return
	 * @author zhangguohua zhanggh@jiuhaohealth.com
	 */
	public static String loadRequestInfo(HttpServletRequest request) {
		RequestInfo reqinf = new RequestInfo();
		loadBasicInfo(request,reqinf);
		loadHeaderInfo(request,reqinf);
		loadParamInfo(request,reqinf);
		loadAttributeInfo(request,reqinf);
		String msg = null;
		try {
			msg = mapper.writeValueAsString(reqinf);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return msg;
	}
	/**
	 * 收集整理请求信息
	 * @param event
	 * @return
	 * @author zhangguohua zhanggh@jiuhaohealth.com
	 */
	@SuppressWarnings("unchecked")
	public static String loadRequestInfo(HttpServletRequest request,String paramJsonString) {
		String msg = null;
		
		try {
			RequestInfo reqinf = new RequestInfo();
			loadBasicInfo(request,reqinf);
			loadHeaderInfo(request,reqinf);
			HashMap<String,Object> params = (HashMap<String,Object>)mapper.readValue(paramJsonString, new TypeReference<HashMap<String,Object>>(){});
			reqinf.setParameters(params);
			loadAttributeInfo(request,reqinf);
			msg = mapper.writeValueAsString(reqinf);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		return msg;
	}
	
	/**
	 * 整理请求的基本信息
	 * @param request
	 * @param reqinf
	 */
	private static void loadBasicInfo(HttpServletRequest request,RequestInfo reqinf){
		reqinf.setServer(request.getServerName());
		reqinf.setProtocol(request.getProtocol());
		reqinf.setMethod(request.getMethod());
		reqinf.setServerPort(""+request.getServerPort());
		reqinf.setContextPath(request.getContextPath());
		reqinf.setServletPath(request.getServletPath());
		reqinf.setRequestURI(request.getRequestURI());
		reqinf.setQueryString(request.getQueryString());
		reqinf.setRemoteAddr(request.getRemoteAddr());
		reqinf.setRemotePort(""+request.getRemotePort());
		reqinf.setTimestamp(System.currentTimeMillis());
	}
	
	/**
	 * 整理header信息
	 * @param request
	 * @param reqinf
	 */
	private static void loadHeaderInfo(HttpServletRequest request,RequestInfo reqinf){
		Map<String,Object> header = new HashMap<String,Object>();
		Enumeration<?> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
	        String key = headerNames.nextElement().toString();
	        String value = request.getHeader(key);
	        header.put(key, value);
	    }
		reqinf.setHeader(header);
	}
	
	/**
	 * 整理parameter信息
	 * @param request
	 * @param reqinf
	 */
	private static void loadParamInfo(HttpServletRequest request,RequestInfo reqinf){
		Map<String,Object> parameters = new HashMap<String,Object>();
		Enumeration<?> paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
	        String key = paramNames.nextElement().toString();
	        String value = request.getParameter(key);
	        parameters.put(key, value);
	    }
		reqinf.setParameters(parameters);
	}
	
	/**
	 * 整理attribute信息
	 * @param request
	 * @param reqinf
	 */
	private static void loadAttributeInfo(HttpServletRequest request,RequestInfo reqinf){
		Map<String,Object> attributes = new HashMap<String,Object>();
//		Enumeration<?> attriNames = request.getAttributeNames();
//		while (attriNames.hasMoreElements()) {
//	        String key = attriNames.nextElement().toString();
//	        String value = request.getAttribute(key).toString();
//	        attributes.put(key, value);
//	    }
		reqinf.setAttributes(attributes);
	}
}
